<?php
declare (strict_types = 1);

namespace app\controller\admin;

use app\controller\admin\AdminBaseController;
use app\model\admin\SysDictData as SysDictDataModel;
use app\model\admin\SysDictType as SysDictTypeModel;

use think\Request;

class SysDictType extends AdminBaseController
{
     /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $field = "*";
        $data = SysDictTypeModel::scope('where', $request->param())
            ->scope('dataFilter')
            ->field($field)
            ->order('id','desc')
            ->paginate([
                'list_rows' => $this->limit,
                'page' => $this->page,
                'var_page' => $this->pageVar
            ]);
        
        return successPage($data);
    }

    /**
     * 获取字典类型数据
     *
     * @return \think\Response
     */
    public function getData(Request $request)
    {
        $type = $request->param('type');

        $data = SysDictDataModel::where('dict_type', $type)->order('dict_sort', 'asc')->select();
        return success($data);
    }

    public function save(Request $request)
    {
        $param = $request->param();
        $dict_name = $param['dict_name'] ?? '';
        $dict_type = $param['dict_type'] ?? '';
        $remark = $param['remark'] ?? '';
        $status = $param['status']  ?? '0';

        $data = SysDictTypeModel::create([
            'dict_name' => $dict_name,
            'dict_type' => $dict_type,
            'remark' => $remark,
            'status' => $status,
        ]);
        if($data->id){
            return success($data);
        }
        return error('添加失败');
    }

    public function show(Request $request)
    {
        $id = $request->param('id');
        $data = SysDictTypeModel::where('id', $id)->scope('dataFilter')->find();

        if(!$data){
            return error('数据不存在');
        }
        return success($data);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $id = $request->param('id');
        if(!$id){
            return error('缺少必要参数');
        }
        
        $model = SysDictTypeModel::where('id', $id)->scope('dataFilter')->find();

        $data = $request->only(['dict_name','dict_type','remark','status']);
        $ret = $model->save($data);
        if(!$ret){
            return error('更新失败');
        }
        return success();
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(Request $request)
    {
        $id = $request->param('id', '');
        if(!$id){
            return error('缺少必要参数');
        }
        $ids = explode(',', $id);

        $ret = SysDictTypeModel::destroy($ids);
        if(!$ret){
            return error('删除失败');
        }
        return success();
    }

    public function optionSelect(Request $request){
        $data = SysDictTypeModel::scope('dataFilter')
            ->order('id','desc')
            ->select();
        return success($data);
    }
}
